package ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import tools.PointCloudViewer.RENDER_TYPE;
import ui.NumericKeyAdapter;

public class MSTClusteringFrame extends JDialog
{
	private JTextField txtDrop; // drop all edges with less than this weight when doing the clustering
	private JTextField txtFrac; // fraction of edges to consider when creating the initial graph
	private JTextArea statsMST;
	private JButton btnFindMST;
	private JButton btnRunClustering;
	
	
	private Window_ViewerApp parent;
	public MSTClusteringFrame(Window_ViewerApp window)
	{
		setTitle("Cylinder Clustering Parameters");
		parent = window;
		
		setupUI();
		
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	
	private void setupUI()
	{
		txtDrop = new JTextField("2", 5);
		txtDrop.addKeyListener(new NumericKeyAdapter());
		
		txtFrac = new JTextField("100", 5);
		txtFrac.addKeyListener(new NumericKeyAdapter());
		
		statsMST = new JTextArea(5, 15);

		btnFindMST = new JButton("Find MST");
		btnFindMST.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				parent.canvas.viewer.find_mst();
				parent.canvas.viewer.render_mode = RENDER_TYPE.MST;
				parent.canvas.viewer.updateComplete = true;
			}
		});
		
		btnRunClustering = new JButton("Run Clustering");
		btnRunClustering.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				double eps = Double.parseDouble(txtDrop.getText());
				parent.canvas.viewer.mst_clustering(eps);
			}
		});
		
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.WEST;

		
		c.gridx = 0;
		c.gridy = 0;
		add(new JLabel("Complete Graph Edge Fraction: "), c);
		
		c.gridx = 1;
		c.gridy = 0;
		add(txtFrac, c);

		c.gridx = 0;
		c.gridy = 1;
		c.weightx = 2.0;
		add(btnFindMST, c);
		
		c.gridx = 0;
		c.gridy = 2;
		add(new JLabel("Drop Edge Weight: "), c);
		
		c.gridx = 1;
		c.gridy = 2;
		add(txtDrop, c);
		
		c.gridx = 0;
		c.gridy = 3;
		c.weightx = 2.0;
		add(btnRunClustering, c);
		
		pack();
	}

}
